package com.ggkt.common.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 查询条件请求实体
 * @version 		V1.0
 * @author          ggkt
 * @date 			2021-08-12
 */
@Data
@ToString
public class QueryFilter<T> {
    //    查询关键字
    private String kwd;
    //    json数据
    private JSONObject json;
    //    每页显示数
    private Integer pageSize = 20;
    //    当前页码
    private Integer pageNo = 1;
    /**
     * 排序filed > asc/desc
     */
    private LinkedHashMap<String, String> sort;
    /**
     * 过滤字段 filed = value
     */
    private LinkedList<QueryFilterField> fieldList;

    /**
     * 无排序
     *
     * @return
     */
    public Page<T> toPage() {
        Page<T> page = new Page<T>();
        page.setSize(this.pageSize);
        page.setCurrent(this.getPageNo());
        return page;
    }

    /**
     * 构建查询条件
     * @param fillOrder true:填充排序 默认填充排序
     * @return
     */
    public QueryWrapper<T> toWrapper(boolean... fillOrder) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        //字段处理
        if (fieldList != null && fieldList.isEmpty() == false) {
            for (QueryFilterField field : fieldList) {
                String cls=field.getField();
                if (StringUtils.isNoneBlank(cls)) {
                    Object val=field.getVal();
                    String exp=field.getExp();
                    if(StringUtils.isEmpty(exp)){
                        continue;
                    }
                    exp=exp.toLowerCase().trim();
                    if ("=".equals(exp) && val != null) {
                        queryWrapper.eq(cls, val);
                    } else if ("!=".equals(exp) && val != null) {
                        queryWrapper.ne(cls, val);
                    } else if (">".equals(exp) && val != null) {
                        queryWrapper.gt(cls, val);
                    } else if ("<".equals(exp) && val != null) {
                        queryWrapper.lt(cls, val);
                    } else if (">=".equals(exp) && val != null) {
                        queryWrapper.ge(cls, val);
                    } else if ("<=".equals(exp) && val != null) {
                        queryWrapper.le(cls, val);
                    } else if ("isnull".equals(exp)) {
                        queryWrapper.isNull(field.getField());
                    } else if ("isnotnull".equals(exp)) {
                        queryWrapper.isNotNull(field.getField());
                    } else if ("like".equals(exp) && val != null && StringUtils.isNotEmpty(val.toString())) {
                        queryWrapper.like(cls, val);
                    } else if ("likeleft".equals(exp) && val != null && StringUtils.isNotEmpty(val.toString())) {
                        queryWrapper.likeLeft(cls, val);
                    } else if ("likeright".equals(exp) && val != null && StringUtils.isNotEmpty(val.toString())) {
                        queryWrapper.likeRight(cls, val);
                    } else if ("notlike".equals(exp) && val != null && StringUtils.isNotEmpty(val.toString())) {
                        queryWrapper.notLike(cls, val);
                    } else if ("between".equals(exp) && val != null &&( val instanceof List || val instanceof JSONArray)) {
                        List objs = (List) val;
                        if(objs.get(0)!=null&&objs.get(1)!=null){
                            queryWrapper.between(cls, objs.get(0), objs.get(1));
                        }else if(objs.get(0)!=null){
                            queryWrapper.ge(cls,objs.get(0));
                        }else if(objs.get(1)!=null){
                            queryWrapper.le(cls,objs.get(1));
                        }
                    }
                }
            }
        }
//        if (queryWrapper.getExpression().getNormal().isEmpty()) {//判断是否有查询条件
//            queryWrapper.apply("1=1");
//        }
        if(fillOrder!=null&&fillOrder.length>0&&fillOrder[0]==false){
            return queryWrapper;
        }
        return fillOrder(queryWrapper);
    }

    /**
     * 非分页查询时调用
     *
     * @param queryWrapper
     * @return
     */
    public QueryWrapper<T> fillOrder(QueryWrapper<T> queryWrapper) {
        //排序处理
        if (sort != null && sort.isEmpty() == false) {
            List<String> ascSort = new ArrayList<String>();
            List<String> descSort = new ArrayList<String>();
            for (String key : sort.keySet()) {
                String val = sort.get(key);
                if (StringUtils.isNoneBlank(val)) {
                    if ("asc".equals(val.toLowerCase())) {
                        ascSort.add(key);
                    } else {
                        descSort.add(key);
                    }
                }
            }
            if (ascSort != null && ascSort.isEmpty() == false) {
                String[] sortStr = new String[ascSort.size()];
                int i = 0;
                for (String f : ascSort) {
                    sortStr[i++] = f;
                    queryWrapper.orderByAsc(f);
                }
//                queryWrapper.orderByAsc(sortStr);
            }
            if (descSort != null && descSort.isEmpty() == false) {
                String[] sortStr = new String[descSort.size()];
                int i = 0;
                for (String f : descSort) {
                    sortStr[i++] = f;
                    queryWrapper.orderByDesc(f);
                }
//                queryWrapper.orderByDesc(sortStr);
            }
        }
        return queryWrapper;
    }
}
